
define([
  './themes/lang/' + window.YUFP_SYS_CONFIG.language + '/pages/example/custCi/custCi.js'
], function (require, exports) {
  exports.ready = function (hashCode, data, cite) {
    // 注册该功能要用到的数据字典
    yufp.lookup.reg('DATA_STS,YESNO,SYS_TYPE');
    yufp.lookup.lookupMgr['CUST_TYPE'] = [
      {key: '01', value: this.$t('custCi.gr')},
      {key: '02', value: this.$t('custCi.jg')}
    ];
    yufp.lookup.lookupMgr['IDENT_TYPE'] = [
      {key: '0101001', value: this.$t('custCi.sfz')},
      {key: '0101002', value: this.$t('custCi.lssfz')}
    ];
    yufp.custom.vue({
      el: '#custAccount',
      // 以m_开头的属性为UI数据不作为业务数据，否则为业务数据
      data: function () {
        var me = this;

        return {
          mainGrid: {
            currentRow: null,
            data: null,
            total: null,
            dataUrl: backend.actService + '/api/fcicust/index',
            height: yufp.custom.viewSize().height - 140,
            queryFields: [
              {placeholder: this.$t('custCi.khbm'), field: 'custId', type: 'input'}
            ],
            queryButtons: [
              {
                label: this.$t('custCi.cx'),
                op: 'submit',
                type: 'primary',
                icon: 'search',
                click: function (model, valid) {
                  if (valid) {
                    var param = { condition: JSON.stringify(model) };
                    me.$refs.mytable.remoteData(param);
                  }
                }
              },
              {label: this.$t('custCi.zz'), op: 'reset', type: 'primary', icon: 'information'}
            ],
            paging: {
              page: 1,
              size: 10
            },
            tableColumns: [
              { label: this.$t('custCi.khbh'), prop: 'custId', sortable: 'custom', resizable: true },
              { label: this.$t('custCi.khmc'), prop: 'custName', sortable: true, resizable: true },
              { label: this.$t('custCi.khlx'), prop: 'custType', sortable: true, resizable: true, dataCode: 'CUST_TYPE'},
              { label: this.$t('custCi.khzt'), prop: 'custStatus', sortable: true, resizable: true, dataCode: 'DATA_STS'},
              { label: this.$t('custCi.zjlx'), prop: 'identType', sortable: true, resizable: true, dataCode: 'IDENT_TYPE'},
              { label: this.$t('custCi.zjbh'), prop: 'identNo', sortable: true, resizable: true},
              { label: this.$t('custCi.lxhm'), prop: 'telphone', sortable: true, resizable: true},
              { label: this.$t('custCi.dz'), prop: 'address', sortable: true, resizable: true}

            ]
          },
          consumer: {
            visible: false,
            formDisabled: false,
            dataUrl: '',
            height: yufp.custom.viewSize().height - 140,
            formdata: {
              custId: null,
              custName: null,
              moneyAmount: null,
              scoreAmount: null,
              amount: null
            },
            tableColumns: [
              { label: this.$t('custCi.khbh'), prop: 'custId', sortable: 'custom', resizable: true },
              { label: this.$t('custCi.jyzje'), prop: 'tradTotalMoney', sortable: true, resizable: true, align: 'right'},
              { label: this.$t('custCi.syjf'), prop: 'scoreAmt', sortable: true, resizable: true, dataCode: 'CUST_TYPE', align: 'right'},
              { label: this.$t('custCi.syzj'), prop: 'tradMoney', sortable: true, resizable: true, dataCode: 'DATA_STS', align: 'right'},
              // { label: '交易状态', prop: 'transState',sortable: true,  resizable: true, dataCode: "IDENT_TYPE"},
              { label: this.$t('custCi.jysj'), prop: 'tradDate', sortable: true, resizable: true}
            ]
          },
          updateFields: [{
            columnCount: 2,
            fields: [
              { field: 'custId',
                label: this.$t('custCi.khbh'),
                rules: [
                  {required: true, message: this.$t('custCi.btx'), trigger: 'blur'},
                  {max: 30, message: this.$t('custCi.srzgc'), trigger: 'blur'}
                ]},
              { field: 'custName', label: this.$t('custCi.khmc')},
              { field: 'custType',
                label: this.$t('custCi.khlx'),
                type: 'select',
                dataCode: 'CUST_TYPE',
                rules: [
                  {required: true, message: this.$t('custCi.btx'), trigger: 'change'}
                ] },
              { field: 'custStatus',
                label: this.$t('custCi.khzt'),
                type: 'select',
                dataCode: 'DATA_STS',
                rules: [
                  {required: true, message: this.$t('custCi.btx'), trigger: 'blur'}
                ]},
              { field: 'identType',
                label: this.$t('custCi.zjlx'),
                type: 'select',
                dataCode: 'IDENT_TYPE',
                rules: [
                  {required: true, message: this.$t('custCi.btx'), trigger: 'blur'}
                ]},
              { field: 'identNo',
                label: this.$t('custCi.zjhm'),
                rules: [
                  {required: true, message: this.$t('custCi.btx'), trigger: 'blur'},
                  {max: 18, message: this.$t('custCi.srzgc'), trigger: 'blur'}
                ]},
              { field: 'telphone',
                label: this.$t('custCi.lxhm'),
                rules: [
                  {required: true, message: this.$t('custCi.btx'), trigger: 'blur'},
                  {max: 100, message: this.$t('custCi.srzgc'), trigger: 'blur'}
                ]
              },
              {
                field: 'address',
                label: this.$t('custCi.dz'),
                rules: [
                  {required: true, message: this.$t('custCi.btx'), trigger: 'blur'},
                  {max: 100, message: this.$t('custCi.srzgc'), trigger: 'blur'}
                ]
              }
            ]
          }],
          dataParams: {},
          formDisabled: false,
          dialogFormVisible: false,
          dialogStatus: false,
          textMap: {
            update: this.$t('custCi.xg'),
            create: this.$t('custCi.xz'),
            consumer: this.$t('custCi.xf')
          }
        };
      },
      mounted: function () {
      },
      methods: {
        rowClickFn: function (row, index) {
          this.mainGrid.currentRow = this.$refs.mytable.selections[0];
        },
        queryMainGridFn: function () {
          var me = this;
          me.$refs.mytable.remoteData();
        },
        openCreateFn: function () {
          this.dialogStatus = 'create';

          this.dialogFormVisible = true;
          this.formDisabled = false;
          this.$nextTick(function () {
            this.$refs.myform.resetFields();
          });
        },
        openEditFn: function (row) {
          this.dialogStatus = 'update';
          this.dialogFormVisible = true;
          this.formDisabled = false;

          this.$nextTick(function () {
            this.$refs.myform.resetFields();
            yufp.extend(this.$refs.myform.formModel, row);
          });
        },
        saveCreateFn: function (formName) {
          var vue = this;
          var myform = vue.$refs.myform;
          var formFlag = true;

          myform.validate(function (valid) {
            formFlag = valid;
          });

          if (formFlag) {
            var comitData = {
              custId: null,
              custName: null,
              custType: null,
              custStatus: null,
              identType: null,
              identNo: null,
              telphone: null,
              address: null
            };
            yufp.extend(comitData, myform.formModel);

            yufp.service.request({
              method: 'POST',
              url: backend.actService + '/api/fcicust/create',
              data: comitData,
              callback: function (code, message, response) {
                if (response.code === '0') {
                  vue.dialogFormVisible = false;
                  vue.$message({message: vue.$t('custCi.khkhcg'), type: 'info'});
                  vue.queryMainGridFn();
                } else {
                  vue.$message({message: response.message, type: response.level});
                }
              }
            });
          } else {
            this.$message({message: this.$t('custCi.qjcsrxsfhf'), type: 'warning'});
            return false;
          }
        },
        saveEditFn: function (formName) {
          var vue = this;
          var myform = vue.$refs.myform;
          var formFlag = true;
          myform.validate(function (valid) {
            formFlag = valid;
          });
          if (!formFlag) {
            this.$message({message: this.$t('custCi.qjcsrxsfhf'), type: 'warning'});
            return false;
          }

          var comitData = {
            custId: null,
            custName: null,
            custType: null,
            custStatus: null,
            identType: null,
            identNo: null,
            telphone: null,
            address: null
          };

          yufp.extend(comitData, myform.formModel);

          yufp.service.request({
            method: 'POST',
            url: backend.actService + '/api/fcicust/update',
            data: comitData,
            callback: function (code, message, response) {
              if (response.code === '0') {
                vue.dialogFormVisible = false;
                vue.$message({message: vue.$t('custCi.sjbccg')});
                vue.queryMainGridFn();
              } else {
                vue.$message({message: response.message, type: response.level});
              }
            }
          });
        },
        handleModify: function (status) {
          var row;
          if (this.$refs.mytable.selections.length !== 1) {
            this.$message({message: this.$t('custCi.qxzytsj'), type: 'warning'});
            return;
          } else {
            row = this.$refs.mytable.selections[0];
          }
          if (status === 'edit') {
            this.mainGrid.currentRow = row;
            this.openEditFn(row);
          } else if (status === 'consumer') {
            this.mainGrid.currentRow = row;
            this.openConsumerFn(row);
          }
        },
        openConsumerFn: function (row) {
          var me = this;

          me.consumer.formdata = {
            custId: null,
            custName: null,
            moneyAmount: null,
            scoreAmount: null,
            amount: null
          };

          this.dialogStatus = 'consumer';
          this.consumer.visible = true;

          this.queryScoreTrans(row);
        },
        queryScoreTrans: function (row) {
          if (row == '') {
            row = this.consumer.formdata;
          }
          var me = this;

          this.consumer.dataUrl = backend.scoreService + '/api/scoretrans/index?custId=' + row.custId;
          this.$nextTick(function () {
            this.$refs.mytableConsumer.remoteData();

            me.consumer.formdata.custId = row.custId;
            me.consumer.formdata.custName = row.custName;

            // 查询余额
            yufp.service.request({
              method: 'GET',
              url: backend.actService + '/api/act/index?custId=' + row.custId,
              data: null,
              callback: function (code, message, response) {
                if (response.code === '0' && response.data != null) {
                  me.consumer.formdata.moneyAmount = response.data[0].amount;
                } else {
                  me.consumer.formdata.moneyAmount = 0;
                  // vue.$message({message: response.message, type: response.level});
                }
              }
            });

            // 查询积分
            yufp.service.request({
              method: 'GET',
              url: backend.scoreService + '/api/score/index?custId=' + row.custId,
              data: null,
              callback: function (code, message, response) {
                if (response.code === '0' && response.data != null) {
                  me.consumer.formdata.scoreAmount = response.data[0].amount;
                } else {
                  me.consumer.formdata.scoreAmount = 0;
                  // vue.$message({message: response.message, type: response.level});
                }
              }
            });
          });
        },
        postConsumer: function () {
          var me = this;

          // 消费
          yufp.service.request({
            method: 'GET',
            url: backend.compositeService + '/api/zy/transfer?custId=' + me.consumer.formdata.custId + '&amount=' + me.consumer.formdata.amount,
            data: null,
            callback: function (code, message, response) {
              if (response.code === '0') {
                me.$message({message: me.$t('custCi.tjcg'), type: 'info'});
                me.consumer.formdata.amount = '';
              } else {
                me.consumer.formdata.amount = '';
                var msg = response.message;
                if (msg.indexOf('message') > 0) {
                  msg = msg.substr(msg.indexOf('message') + 7);
                  if (msg.indexOf('path') > 0) {
                    msg = msg.substr(0, msg.indexOf('path'));
                  }
                  var x = msg.split('"');

                  if (x != null && x.length > 3) {
                    msg = x[2];
                  }
                }

                me.$message({message: msg, type: 'error'});
              }
            }
          });
        }
      }
    });
  };

  // 消息处理
  exports.onmessage = function (type, message) { };

  // page销毁时触发destroy方法
  exports.destroy = function (id, cite) {};
});
